package canglang.scan.core.base.action;

import com.opensymphony.xwork2.ActionSupport;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@SuppressWarnings("unchecked")
public class BaseAction extends ActionSupport {

    /**
	 * 
	 */
    private static final long serialVersionUID = 2301934735618327087L;

    protected final Log log = LogFactory.getLog(getClass());

    /**
     * Convenience method to get the request
     * 
     * @return current request
     */
    protected HttpServletRequest getRequest () {
        return ServletActionContext.getRequest();
    }

    /**
     * Convenience method to get the response
     * 
     * @return current response
     */
    protected HttpServletResponse getResponse () {
        return ServletActionContext.getResponse();
    }

    /**
     * Convenience method to get the session. This will create a session if one
     * doesn't exist.
     * 
     * @return the session from the request (request.getSession()).
     */
    protected HttpSession getSession () {
        return getRequest().getSession();
    }

    /**
     * Save the message in the session, appending if messages already exist
     * 
     * @param msg the message to put in the session
     */
    protected void saveMessage (String msg) {
        List messages = (List) getRequest().getSession().getAttribute("messages");
        if (messages == null) {
            messages = new ArrayList();
        }
        messages.add(msg);
        getRequest().getSession().setAttribute("messages", messages);
    }
  
}
